﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class DatHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((DataTable)Session["giohang"] != null)
        {
            DataTable tb_giohang;
            tb_giohang = (DataTable)Session["giohang"];
            decimal tongtien = 0;
            foreach (DataRow item in tb_giohang.Rows)
            {
                tongtien += Convert.ToDecimal(item["TONGTIEN"]);
            }
            txtTongthanhtoan.Text = tongtien.ToString("0,0.0");
            GridView1.DataSource = tb_giohang;
            GridView1.DataBind();
        }
        else
        {

        }
        // Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
        //Response.Expires = -1500;
        // Response.CacheControl = "no-cache";
        Session.Timeout = 1;
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            OTOWeb.DataOut dto = new OTOWeb.DataOut();
            OTOWeb.KHACHHANG_ENTITY khe = new OTOWeb.KHACHHANG_ENTITY();
            khe.TENKH = txtHoTen.Text;
            khe.DIACHI = txtDC.Text;
            khe.DIENTHOAI = txtDT.Text;
            khe.EMAIL = txtEm.Text;
            khe.MASOTHUE = txtThue.Text;
            int idkh = dto.Web_ThemKH(khe);

            OTOWeb.DONHANG_ENTITY enti = new OTOWeb.DONHANG_ENTITY();
            string mahd = "HD" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year + DateTime.Now.Minute + DateTime.Now.Second;
            enti.MAHD = mahd;
            enti.MAKH = idkh;
            enti.NGAYHD = DateTime.Now;
            enti.THANHTIEN = Convert.ToDecimal(txtTongthanhtoan.Text);
            dto.Web_ThemDONHANG(enti);
            if ((DataTable)Session["giohang"] != null)
            {
                DataTable tb_giohang;
                tb_giohang = (DataTable)Session["giohang"];
                foreach (DataRow item in tb_giohang.Rows)
                {
                    OTOWeb.CHITIET_DONHANG_ENTITY entict = new OTOWeb.CHITIET_DONHANG_ENTITY();
                    entict.MAHD = mahd;
                    entict.MAXE = item["MAXE"].ToString();
                    entict.SOLUONG = Convert.ToInt32(item["SOLUONG"]);
                    entict.DONGIA = Convert.ToDecimal(item["TONGTIEN"]);
                    dto.Web_ThemDONHANGCT(entict);
                }

            }
            Response.Redirect("Success.aspx");

        }
            catch (Exception ex)
            {
                throw ex;
            }

    }
}